<template>
  <div>
    <!-- Header 区域 -->
    <van-nav-bar fixed
                 title="文章详情"
                 left-arrow
                 @click-left="$router.back()" />
    <!-- obj的初始值是{}，所以obj.title是undefined时，证明网络请求还没回来 -->
    <!-- 文章详情和评论列表和加载是互斥的，先让van-loading在原地转圆圈实现 -->
    <div class="div"
         v-if="obj.title===undefined">
      <van-loading type="spinner"
                   color="#1989fa"
                   class="loading" />
    </div>
    <div v-else>
      <!-- 文章信息区域 -->
      <div class="article-container">
        <!-- 文章标题 -->
        <h1 class="art-title">{{obj.title}}</h1>

        <!-- 用户信息 -->
        <van-cell center
                  :title="obj.aut_name"
                  :label="formatTime(obj.pubdate)">
          <template #icon>
            <img :src="obj.aut_photo"
                 alt=""
                 class="avatar">
          </template>
          <template #default>
            <div>
              <!-- is_followed为true代表显示,表示以前关注过 -->
              <van-button type="info"
                          v-if="obj.is_followed"
                          size="mini"
                          @click="followFn(true)">已关注</van-button>
              <van-button icon="plus"
                          type="info"
                          size="mini"
                          v-else
                          @click="followFn(false)"
                          plain>关注</van-button>
            </div>
          </template>
        </van-cell>

        <!-- 分割线 -->
        <van-divider></van-divider>

        <!-- 文章内容 -->
        <div class="art-content"
             v-html="obj.content"></div>

        <!-- 分割线 -->
        <van-divider>End</van-divider>

        <!-- 点赞 -->
        <div class="like-box">
          <van-button icon="good-job"
                      type="danger"
                      v-if="obj.attitude>0"
                      @click="attitudeFn(true)"
                      size="small">已点赞</van-button>
          <van-button icon="good-job-o"
                      type="danger"
                      plain
                      v-else
                      @click="attitudeFn(false)"
                      size="small">点赞</van-button>
        </div>
      </div>
      <!-- 评论列表区 -->
      <CommentList :arr="commentList"
                   ref="com"
                   :allCount="allCommentCount"
                   @more="moreFn"
                   @like="likeFn"></CommentList>
    </div>
  </div>

</template>

<script>
import { articleDetailAPI, authorFollowingAPI, authorCanncelAPI, articleAttitudeAPI, articleUnAttitudeAPI, commentListAPI, commentLikeAPI, commentUnLikeAPI } from '@/api'
import { formatTime } from '@/utils/date'
import CommentList from './components/CommentList.vue'
// 分页思路：第一页传null,后台返回第一页数据，以及下一页的起始数据的id
export default {
  data () {
    return {
      obj: {}, // 文章对象
      commentList: [], // 评论数据
      offset: null, // 评论列表-分页（偏移量last-id）
      allCommentCount: 0// 评论的总数量

    }
  },
  components: {
    CommentList
  },
  async created () {
    // 获取指定文章
    const [, res] = await articleDetailAPI({
      artId: this.$route.query.art_id
    })
    this.obj = res.data.data
    // 获取评论列表
    this.getCommentList()
  },

  methods: {
    formatTime: formatTime,
    // 关注
    async followFn (bool) {
      if (bool) {
        // 点击的是已关注，代表之前关注过，要实现取消关注
        const [err] = await authorCanncelAPI({
          autId: this.obj.aut_id
        })
        // 没有错误，在影响页面状态
        if (!err) this.obj.is_followed = false
      } else {
        // 点击的是关注按钮
        const [err] = await authorFollowingAPI({
          autId: this.obj.aut_id
        })
        if (!err) this.obj.is_followed = true
      }
    },
    // 点赞
    async attitudeFn (bool) {
      if (bool) {
        // 点击的是已点赞，代表之前点赞过，要实现取消点赞
        const [err] = await articleUnAttitudeAPI({
          artId: this.obj.art_id
        })
        // 没有错误，在影响页面状态
        if (!err) this.obj.attitude = false
      } else {
        // 点击的是点赞按钮
        const [err] = await articleAttitudeAPI({
          artId: this.obj.art_id
        })
        if (!err) this.obj.attitude = true
      }
    },
    // 评论点赞/取消点赞
    async likeFn (bool, commontObj) {
      if (bool) {
        // 点击的是红心，要取消点赞

        const [err] = await commentUnLikeAPI({
          commendId: commontObj.com_id
        })
        if (!err) commontObj.is_liking = false
      } else {
        // 点击的是灰心，要点亮

        const [err] = await commentLikeAPI({
          commendId: commontObj.com_id
        })
        if (!err) commontObj.is_liking = true
      }
    },
    // 评论列表加载更多
    async moreFn () {
      this.getCommentList()
    },
    // 获取评论列表
    async getCommentList () {
      const [, commentRes] = await commentListAPI({
        artId: this.$route.query.art_id,
        offset: this.offset

      })
      // 如果为无数据
      if (commentRes.data.data.results.length === 0) { this.$refs.com.finished = true } else {
        // 如果有数据
        this.offset = commentRes.data.data.last_id
      }
      this.allCommentCount = commentRes.data.data.total_count
      // 合并数据
      this.commentList = [...this.commentList, ...commentRes.data.data.results]
      // 加载完修改loading状态
      this.$refs.com.loading = false

      console.log(commentRes)
    }

  }
}
</script>

<style scoped lang="less">
.article-container {
  padding: 10px;
  margin-top: 46px;
}
.art-title {
  font-size: 16px;
  font-weight: bold;
  margin: 10px 0;
}

.art-content {
  font-size: 12px;
  line-height: 24px;
  width: 100%;
  overflow-x: scroll;
  word-break: break-all;
}

.van-cell {
  padding: 5px 0;
  &::after {
    display: none;
  }
}

.avatar {
  width: 60px;
  height: 60px;
  border-radius: 50%;
  background-color: #f8f8f8;
  margin-right: 5px;
  border: none;
}

.like-box {
  display: flex;
  justify-content: center;
}
.loading {
  margin-top: 50px;
}
.div {
  text-align: center;
}
</style>
